<template>
  <el-dialog
    :title="title"
    :visible.sync="innerVisible"
    width="600px"
    @close="handleCancel"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="formRef"
      label-width="180px"
    >
      <el-form-item label="企业名称" prop="name">
        <el-select
          v-model="form.name"
          :disabled="isDetail"
          placeholder="请选择"
          filterable
          allow-create
          @change="handleSelectChange"
        >
          <el-option
            v-for="item in enterpriseOptions"
            :key="item.socialCode"
            :label="item.name"
            :value="item.name"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        label="统一社会信用代码"
        prop="socialCode"
      >
        <el-input
          v-model="form.socialCode"
          placeholder="请输入"
          :disabled="isDetail || isExisting"
        />
      </el-form-item>

      <el-form-item label="类型">
        <el-select
          v-model="form.type"
          :disabled="isDetail"
          placeholder="请选择"
        >
          <el-option
            v-for="item in enterpriseTypeOptions"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <div
      slot="footer"
      class="dialog-footer"
      v-if="!isDetail"
    >
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" @click="handleConfirm"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import {
  enterpriseTypeOptions,
  isScaleEnterpriseOptions,
  isFixedInvestmentOptions
} from '@/utils/project/data'
import {
  addEnterpriseEconomic,
  enterpriseInfoQuery,
  addEnterpriseInfo
} from '@/api/dataManagement'
import dayjs from 'dayjs'

export default {
  name: 'AddEnterpriseEconomicDialog',
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    title: {
      type: String,
      default: '填写信息'
    },
    isDetail: {
      type: Boolean
    },
    currentData: {
      type: Object
    },
    isEdit: {
      type: Boolean
    },
    year: String,
    month: String
  },
  data() {
    return {
      form: {
        pkid: '',
        name: '',
        socialCode: '',
        type: '',
        enterpriseId: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入企业名称',
            trigger: 'blur'
          }
        ],
        socialCode: [
          {
            required: true,
            message: '请输入统一社会信用代码',
            trigger: 'blur'
          }
        ]
      },
      innerVisible: false,
      enterpriseOptions: [],
      addEnterpriseInfo: {
        name: '',
        socialCode: '',
        type: '',
        enterpriseId: ''
      },
      isExisting: false
    }
  },
  watch: {
    visible(val) {
      this.innerVisible = val
    },
    innerVisible(val) {
      this.$emit('update:visible', val)
    },
    currentData(val) {
      this.getEnterpriseOptions()
      if (this.isEdit) {
        this.form = { ...val }
        this.isExisting = true
      } else {
        this.form = {
          pkid: '',
          name: '',
          socialCode: '',
          type: '',
          enterpriseId: ''
        }
        this.isExisting = false
      }
    }
  },
  computed: {
    enterpriseTypeOptions() {
      return enterpriseTypeOptions
    },
    isScaleEnterpriseOptions() {
      return isScaleEnterpriseOptions
    },
    isFixedInvestmentOptions() {
      return isFixedInvestmentOptions
    }
  },
  mounted() {
    this.getEnterpriseOptions()
  },
  methods: {
    handleSelectChange(val) {
      const value = String(val).trim()
      this.isExisting = this.enterpriseOptions.some(
        (item) => item.name === value
      )
      if (!this.isExisting) {
        this.addEnterpriseInfo.name = value
        this.form.socialCode = ''
      } else {
        const enterpriseInfo = this.enterpriseOptions.find(
          (item) => item.name === value
        )
        this.form.socialCode = enterpriseInfo.socialCode
        this.form.name = enterpriseInfo.name
        this.form.type = enterpriseInfo.type
        this.addEnterpriseInfo.enterpriseId =
          enterpriseInfo.pkid
      }
    },
    getEnterpriseOptions() {
      enterpriseInfoQuery({
        pageIndex: 1,
        pageSize: 999
      }).then((res) => {
        if (res.success) {
          this.enterpriseOptions = res.data.list
        }
      })
    },
    handleCancel() {
      this.$emit('update:visible', false)
    },
    async handleConfirm() {
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          try {
            // 如果是新企业，先添加企业信息
            if (!this.isExisting) {
              this.addEnterpriseInfo.enterpriseId = ''
              this.addEnterpriseInfo.socialCode =
                this.form.socialCode
              this.addEnterpriseInfo.type = this.form.type
              this.addEnterpriseInfo.name = this.form.name

              // const enterpriseRes = await addEnterpriseInfo(this.addEnterpriseInfo)
              // if (!enterpriseRes.success) {
              //   this.$message.error(enterpriseRes.message || '企业信息保存失败')
              //   return
              // }
            }

            this.form.year = this.year
            this.form.month = this.month
            this.form.enterpriseId =
              this.addEnterpriseInfo.enterpriseId

            this.$emit('confirm', { ...this.form })
          } catch (error) {
            this.$message.error('保存失败，请重试')
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '~@/assets/css/dialog.scss';
::v-deep .el-input__inner {
  width: 320px !important;
}
</style>
